﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Future.Contract;

namespace Future.Cache
{
    public class CacheManager
    {
        private Dictionary<Contract.TaskDALEnums, DataAccess.DalBase> _dal = new Dictionary<Contract.TaskDALEnums, DataAccess.DalBase>();


        private static CacheManager _Instance = new CacheManager();

        public static CacheManager shareCacher()
        {
            return _Instance;
        }

        public DataAccess.DalBase getDAL(Contract.TaskDALEnums enums)
        {
            if (_dal.ContainsKey(enums))
            {
                return _dal[enums];
            }

            DataAccess.DalBase retDAL = null;
            switch (enums)
            {
                case Contract.TaskDALEnums.None:
                    break;
                case Contract.TaskDALEnums.DeptDAL:
                    retDAL = new DataAccess.DalBase(new Entities.tbDeptEntity());
                    break;
                case Contract.TaskDALEnums.xHeadDAL:
                    retDAL = new DataAccess.DalBase(new Entities.xHeadEntity());
                    break;
                case Contract.TaskDALEnums.xDetailDAL:
                    retDAL = new DataAccess.DalBase(new Entities.xDetailEntity());
                    break;
            }
            if (retDAL != null)
            {
                _dal.Add(enums, retDAL);
            }
            return retDAL;
        }

        public DataAccess.DalBase getDALByEntity(BaseEntity entity)
        {
            if (entity == null)
            {
                return null;
            }
            Contract.TaskDALEnums enums = Contract.TaskDALEnums.None;
            Type t = entity.GetType();
            switch (t.FullName)
            {
                case "Future.Entities.tbDeptEntity":
                    enums = Contract.TaskDALEnums.DeptDAL;
                    break;
                case "Future.Entities.xHeadEntity":
                    enums = Contract.TaskDALEnums.xHeadDAL;
                    break;
                case "Future.Entities.xDetailEntity":
                    enums = Contract.TaskDALEnums.xDetailDAL;
                    break;
            }
            return getDAL(enums);
        }
    }
}
